package com.longxi.tianjintianbao.widget;

import android.content.Context;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.utils.MyUtils;

public class InputSelectBoxView extends RelativeLayout {
    Context mContext;

    RelativeLayout mRl;
    ImageView mIvHead;
    EditText mEtValue;
    TextView mTvTips;

    int lastNum = 0;
    InputMethodManager imm;
    boolean mIsShowEdit = false;

    public InputSelectBoxView(Context context) {
        super(context);
    }

    public InputSelectBoxView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    /**
     * 设置内容
     * @param image
     * @param hint
     * @param text
     */
    public void setValue(int image, String hint, String text){
        control();
        mIvHead.setImageResource(image);
        mEtValue.setHint(hint);
        mEtValue.setText(text);
        mEtValue.setFocusable(false);
        mEtValue.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
        mEtValue.setClickable(false);
        mEtValue.setEnabled(false);
    }

    /**
     * 初始化普通输入框
     * @param image
     * @param value
     */
    public void setDate(int image, String value){
        control();
        this.lastNum = lastNum;
        setImage(image);
        mEtValue.setHint(value);
    }

    /**
     * 初始化只能点击的edittext
     * @param image
     * @param value
     * @param isOnClick
     */
    public void setDate(int image, String value, boolean isOnClick){
        control();
        this.lastNum = lastNum;
        setImage(image);
        mEtValue.setHint(value);
        if(isOnClick){
            mEtValue.setFocusable(false);
            mEtValue.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
            mEtValue.setClickable(false);
        }
    }

    /**
     * 初始化带有剩余输入数量的输入框
     * @param image
     * @param value
     * @param lastNum
     * @param editTextHeight
     */
    public void setDate(int image, String value, int lastNum, int editTextHeight){
        control();
        this.lastNum = lastNum;

        setImage(image);
        mEtValue.setHint(value);
        showTips(lastNum);

        if(editTextHeight > 0){
            mEtValue.setMinHeight(editTextHeight);
            mEtValue.setGravity(Gravity.TOP|Gravity.LEFT);
        }
    }

    /**
     * 获取输入框
     * @return
     */
    public EditText getEtValue(){
        return mEtValue;
    }

    /**
     * 获取内容
     * @return
     */
    public String getText(){
        String s = "";
        if(mEtValue != null){
            s = mEtValue.getText().toString().trim();
        }
        return s;
    }

    /**
     * 设置左边图片
     * @param image
     */
    private void setImage(int image){
        mIvHead.setImageResource(image);
    }

    /**
     * 设置底部剩余输入数量
     * @param lastNum
     */
    private void showTips(int lastNum){
        mTvTips.setVisibility(VISIBLE);
        SpannableString span = new SpannableString("0/"+lastNum);
        span.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), 0, 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvTips.setText(span);
        mEtValue.addTextChangedListener(textWatcher);
    }

    private void control()
    {
        initialise(); // 实例化内部view
        setViewListener();
    }

    private void initialise() {
        LayoutInflater.from(mContext).inflate(R.layout.view_input_select_box, this);
        mRl = findViewById(R.id.rl);
        mIvHead = findViewById(R.id.ivHead);
        mEtValue = findViewById(R.id.etValue);
        mTvTips = findViewById(R.id.tvTips);
    }

    private void setViewListener() {

    }

    /**
     * 设置内容
     * @param text
     */
    public void setText(String text){
        mEtValue.setText(text);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String cd = String.valueOf(s.length());
            SpannableString span = new SpannableString(cd + "/" + lastNum);
            span.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), 0, cd.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTvTips.setText(span);
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.length() > lastNum){
                MyUtils.showToast("超出限制字数");
                mEtValue.setText(s.toString().substring(0, lastNum));
                mEtValue.setSelection(mEtValue.getText().toString().length());
            }
        }
    };

}
